﻿using System;
using System.Collections;
using System.Xml;
using InfoJetSoft.Service.Entity;
using InfoJetSoft.Service.Util;

namespace InfoJetSoft.Service.Worker
{
	
	class XSNViewParseMasterDetialWorker
	{
		
		public static void  parseMasterDetial(View view)
		{
			string xdPrefix = view.XdPrefix;
			string xslPrefix = view.XslPrefix;
			XmlElement root = view.Doc.DocumentElement;
			string masterNameFilter = "//@" + NamespaceUtil.toFullName(xdPrefix, "masterName");
			XmlNodeList masterNameAttrList = DOMUtil.selectNodes(root, masterNameFilter, 0, view.NsResolver);
			
			ArrayList masterNameList = new ArrayList();
			foreach (XmlAttribute masterNameAttr in masterNameAttrList)
			{
				string masterName = masterNameAttr.Value;

				bool found = false;
				foreach (string name in masterNameList)
				{
					if (masterName.Equals(name))
					{
						found = true;
						break;
					}
				}
				if (found)
				{
					continue;
				}
				masterNameList.Add(masterName);
			}
			
			view.MasterNameList = masterNameList;

			//查找CtrlID和MasterID的匹配定义，用于FormMasterDetailContextWorker中将CtrlID转换为MasterID。
			string linkedToMasterFilter = "//@" + NamespaceUtil.toFullName(xdPrefix, "linkedToMaster");
			XmlNodeList linkedToMasterAttrList = DOMUtil.selectNodes(root, linkedToMasterFilter, 0, view.NsResolver);
			
			Hashtable masterLinkTable = new Hashtable();
			foreach( XmlAttribute linkedToMasterAttr in linkedToMasterAttrList )
			{
				string masterID = linkedToMasterAttr.Value;

				XmlAttribute ctrlIdAttr = linkedToMasterAttr.OwnerElement.GetAttributeNode( NamespaceUtil.toFullName(xdPrefix, "CtrlId") );
				if( ctrlIdAttr != null )
				{
					HashtableUtil.addSafety( masterLinkTable, ctrlIdAttr.Value, masterID );
				}
			}

			view.MasterLinkTable = masterLinkTable;
		}
	}
}